package com.putong35.easyui.somedetail;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.BindingAdapter;
import androidx.databinding.DataBindingUtil;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.appbar.AppBarLayout;
import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentStatePagerItemAdapter;
import com.putong35.common.baseactfrag.activity.LoadableActivity;
import com.putong35.common.baseactfrag.mvp.IMvpView;
import com.putong35.common.customviews.ImageViewPager;
import com.putong35.common.utils.ActivityUtils;
import com.putong35.easyui.R;
import com.putong35.easyui.databinding.ActivitySomeDetailBinding;

import java.util.List;

/**
 * Simple to Introduction
 *
 * @Author: ${USER}
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: ${DATE} ${TIME}
 * @Version: 1.0.0
 */

public class SomeDetailActivity extends LoadableActivity implements IMvpView {

    private static final String ADD_EDIT_VIEWMODEL_TAG = "viewmodel_tag";
    private ActivitySomeDetailBinding activitySomeDetailBinding;

    private int originTabLayoutY;
    private SomeDetailViewModel mViewModelHolder;

    public static void start(Context context) {
        Intent starter = new Intent(context, SomeDetailActivity.class);
        context.startActivity(starter);
    }

    int moveEndY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideToolbar();
        statusTransucentAndLayoutFullscreen();
        activitySomeDetailBinding.tabLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                int[] location = new int[2];
                activitySomeDetailBinding.tabLayout.getLocationOnScreen(location);
                originTabLayoutY = location[1];
                moveEndY = activitySomeDetailBinding.toolbar11.getMeasuredHeight();
                Log.e("pww", "ttt = moveEndY" + moveEndY);
            }
        }, 300);

        activitySomeDetailBinding.toolbar11.setBackgroundResource(R.color.def_theme_color_primary);
        activitySomeDetailBinding.toolbar11.getBackground().setAlpha(255);

        activitySomeDetailBinding.lytHeadContent.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                int[] location = new int[2];
                activitySomeDetailBinding.tabLayout.getLocationOnScreen(location);

                int alpha = 255 - (int) (255.0f * (location[1] - moveEndY) / (originTabLayoutY - moveEndY));
                if (alpha < 0) {
                    alpha = 0;
                } else if (alpha > 255) {
                    alpha = 255;
                }
                Log.e("pww", "alpha = " + alpha);
                activitySomeDetailBinding.toolbar11.getBackground().setAlpha(alpha);
            }
        });

        startLoading();

    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        activitySomeDetailBinding = DataBindingUtil.inflate(inflater, R.layout.activity_some_detail, null, false);
        mViewModelHolder = findOrCreateViewModel();
        activitySomeDetailBinding.setViewModel(mViewModelHolder);
        return activitySomeDetailBinding.getRoot();
    }

    private SomeDetailViewModel findOrCreateViewModel() {
        // In a configuration change we might have a ViewModel present. It's retained using the
        // Fragment Manager.
        @SuppressWarnings("unchecked")
        ViewModelHolder<SomeDetailViewModel> retainedViewModel =
                (ViewModelHolder<SomeDetailViewModel>) getSupportFragmentManager()
                        .findFragmentByTag(ADD_EDIT_VIEWMODEL_TAG);

        if (retainedViewModel != null && retainedViewModel.getViewmodel() != null) {
            // If the model was retained, return it.
            return retainedViewModel.getViewmodel();
        } else {
            // There is no ViewModel yet, create it.
            SomeDetailViewModel viewModel = new SomeDetailViewModel(getApplicationContext(), this);

            // and bind it to this Activity's lifecycle using the Fragment Manager.
            ActivityUtils.addFragmentToActivity(
                    getSupportFragmentManager(),
                    ViewModelHolder.createContainer(viewModel),
                    ADD_EDIT_VIEWMODEL_TAG);
            return viewModel;
        }
    }

    @Override
    public void onStartLoading() {
        mViewModelHolder.startLoading();
    }

    public void setCommentTabTitle(int title) {
        String commentTitle = "评价（" + title + "）";
        View tabView = activitySomeDetailBinding.tabLayout.getTabAt(1);
        if (tabView instanceof TextView) {
            TextView textView = (TextView) tabView;
            textView.setText(commentTitle);
        }

    }


    @BindingAdapter({"image"})
    public static void imageLoader(ImageViewPager imageViewPager, List<String> urls) {
        imageViewPager.setupUrls(urls);

    }

    @BindingAdapter({"pageSize", "tabLayout"})
    public static void setViewPager(ViewPager viewPager, int pageSize, SmartTabLayout tabLayout) {
        Context context = viewPager.getContext();
        if (context instanceof AppCompatActivity) {
            AppCompatActivity activity = (AppCompatActivity) context;
            FragmentPagerItems.Creator creator = FragmentPagerItems.with(activity);
            for (int i = 0; i < pageSize; i++) {
//                creator.add("test", DataBindListFragment.class);
            }

            FragmentStatePagerItemAdapter adapter = new FragmentStatePagerItemAdapter(
                    activity.getSupportFragmentManager(), creator.create());
            viewPager.setAdapter(adapter);
            tabLayout.setViewPager(viewPager);
        }

    }


}
